home *** CD-ROM | disk | FTP | other *** search
- #ifdef MPW
- # pragma segment TCLExtend
- #endif
- /*
- * tclXclock.c --
- *
- * Contains the TCL time and date related commands.
- *-----------------------------------------------------------------------------
- * Copyright 1991-1993 Karl Lehenbauer and Mark Diekhans.
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies. Karl Lehenbauer and
- * Mark Diekhans make no representations about the suitability of this
- * software for any purpose. It is provided "as is" without express or
- * implied warranty.
- *-----------------------------------------------------------------------------
- * $Id: tclXclock.c,v 2.7 1993/08/25 03:15:47 markd Exp $
- *-----------------------------------------------------------------------------
- */
-
- #include "tclExtdInt.h"
-
- #ifdef macintosh
- /*
- ** Difference between Mac and Unix times
- */
- # define TIMEDIFF 0x7c25b080
- #endif
-
- /*
- *-----------------------------------------------------------------------------
- *
- * Tcl_GetclockCmd --
- * Implements the TCL getclock command:
- * getclock
- *
- * Results:
- * Standard TCL results.
- *
- *-----------------------------------------------------------------------------
- */
- int
- Tcl_GetclockCmd (clientData, interp, argc, argv)
- ClientData clientData;
- Tcl_Interp *interp;
- int argc;
- char **argv;
- {
- if (argc != 1) {
- Tcl_AppendResult (interp, tclXWrongArgs, argv[0], (char *) NULL);
- return TCL_ERROR;
- }
- #ifdef macintosh
- sprintf (interp->result, "%lu",
- ((unsigned long)time((time_t *) NULL) - TIMEDIFF) );
- #else
- sprintf (interp->result, "%ld", time ((time_t *) NULL));
- #endif
- return TCL_OK;
- }
-
- /*
- *-----------------------------------------------------------------------------
- *
- * Tcl_FmtclockCmd --
- * Implements the TCL fmtclock command:
- * fmtclock clockval ?format? ?GMT|{}?
- *
- * Results:
- * Standard TCL results.
- *
- *-----------------------------------------------------------------------------
- */
-
- int
- Tcl_FmtclockCmd (clientData, interp, argc, argv)
- ClientData clientData;
- Tcl_Interp *interp;
- int argc;
- char **argv;
- {
- int useGMT = FALSE;
- unsigned long clockVal;
- char *format;
- struct tm *timeDataPtr;
- int stat;
- char buffer[1024];
- #ifdef TCL_USE_TIMEZONE_VAR
- int savedTimeZone;
- char *savedTZEnv;
- #endif
-
- if ((argc < 2) || (argc > 4)) {
- Tcl_AppendResult (interp, tclXWrongArgs, argv [0],
- " clockval ?format? ?GMT|{}?", (char *) NULL);
- return TCL_ERROR;
- }
-
- if (Tcl_GetLong (interp, argv[1], &clockVal) != TCL_OK)
- {
- Tcl_AppendResult(interp, "bad time value \"", argv[1], "\"", NULL);
- return TCL_ERROR;
- }
-
- if ((argc == 4) && (argv [3][0] != '\0')) {
- if (!STREQU (argv [3], "GMT")) {
- Tcl_AppendResult (interp, "expected \"GMT\" or {} got \"",
- argv [3], "\"", (char *) NULL);
- return TCL_ERROR;
- }
- useGMT = TRUE;
- }
-
- if ((argc >= 3) && (argv [2][0] != '\0'))
- format = argv[2];
- else
- format = "%a %b %d %X %Z %Y";
-
- #ifdef TCL_USE_TIMEZONE_VAR
- /*
- * This is a horrible kludge for systems not having the timezone in
- * struct tm. No matter what was specified, they use the global time
- * zone.
- */
- if (useGMT) {
- char *varValue;
-
- varValue = Tcl_GetVar2 (interp, "env", "TZ", TCL_GLOBAL_ONLY);
- if (varValue != NULL)
- savedTZEnv = ckstrdup (varValue);
- else
- savedTZEnv = NULL;
- Tcl_SetVar2 (interp, "env", "TZ", "GMT", TCL_GLOBAL_ONLY);
- savedTimeZone = timezone;
- timezone = 0;
- tzset ();
- }
- #endif
-
- #ifdef macintosh
- clockVal += TIMEDIFF;
- #endif
-
- if (useGMT)
- {
- #ifdef macintosh
- /* UNDONE UNDONE */
- /* On the Mac, in MPW, gmtime() returns NULL!!! */
- /* gmtime ((time_t *) &clockVal); */
- timeDataPtr = localtime ((time_t *) &clockVal);
- #else
- timeDataPtr = gmtime ((time_t *) &clockVal);
- #endif
- }
- else
- {
- timeDataPtr = localtime ((time_t *) &clockVal);
- }
-
- stat = strftime (buffer, sizeof(buffer)-2, format, timeDataPtr);
-
- #ifdef TCL_USE_TIMEZONE_VAR
- if (useGMT) {
- if (savedTZEnv != NULL) {
- Tcl_SetVar2 (interp, "env", "TZ", savedTZEnv, TCL_GLOBAL_ONLY);
- ckfree (savedTZEnv);
- } else {
- Tcl_UnsetVar2 (interp, "env", "TZ", TCL_GLOBAL_ONLY);
- }
- timezone = savedTimeZone;
- tzset ();
- }
- #endif
-
- if (stat <= 0) {
- Tcl_AppendResult (interp, "error formatting time", (char *) NULL);
- return TCL_ERROR;
- }
- else
- {
- Tcl_SetResult(interp, buffer, TCL_VOLATILE);
- }
-
- return TCL_OK;
- }
-